﻿using System;
using System.Threading.Tasks;
using Quartz;

namespace QuartzExt
{
    /// <summary>
    /// 自定义任务基类
    /// </summary>
    public abstract class BaseJob : IJob
    {
        /// <summary>
        /// 任务执行
        /// </summary>
        public abstract void Run(IJobExecutionContext context);
        public Task Execute(IJobExecutionContext context)
        {           
            var key = $"【{context.JobDetail.Key.Group}】{context.JobDetail.Key.Name}";
            return Task.Run(() =>
            {
                try
                {
                    Run(context);                    
                    QuaztzManager.MessageQueue.Enqueue(new JobResult()
                    {
                        Message = $"{key} 执行完毕",
                        Success = true,
                        Time = DateTime.Now
                    });
                }
                catch (Exception e)
                {
                    QuaztzManager.MessageQueue.Enqueue(new JobResult()
                    {
                        Message = $"{key} 执行异常,{e.Message}",
                        Success = true,
                        Time = DateTime.Now
                    });
                }
            });
        }
    }
}
